package realtime.statistic;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @author lit
 * @date 2018/4/9.
 */
public class Main {
    /**
     *
     * @param args 输入参数格式为yyyyMMddHHmm
     */
    public static void main(String[] args){
        try{
            Logger logger = LoggerFactory.getLogger(Main.class);
            // 默认输入为当前5分钟的数据。 输入的时间点始终为时间后点
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String datetime = sdf.format(System.currentTimeMillis());
            String prefix = datetime.substring(0, 10);
            int minute = 5 * ( Integer.valueOf(datetime.substring(10, 12))/5 );
            datetime = prefix + String.format("%02d", minute);

            if( args != null && args.length > 0 ){
                datetime = args[0];
            }
            logger.info("5分钟统计正在处理的时间点是 {},即将处理前十分钟入库的数据", datetime);

            String statTime = sdf1.format(sdf.parse(datetime).getTime());
            String lastStatMonth = getRealtimeStatTime(statTime);

            ProcessTrip pt = new ProcessTrip();
            pt.processTrip(lastStatMonth,statTime);

            ProcessTripIndex pti = new ProcessTripIndex();
            pti.processTripIndex(lastStatMonth,statTime);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 获取上一个统计月份，如现在4月，那上一个统计月份是3月
     * @return eg:'2018-03-01 00:00:00'
     * @param statTime 统计时间 eg:'2018-04-17 10:20:00'
     */
    private static String getRealtimeStatTime(String statTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取当前时间
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(sdf.parse(statTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        int year = calendar.get(Calendar.YEAR);
        //Calendar.MONTH默认值范围是0-11
        int month = calendar.get(Calendar.MONTH);

        calendar.set(year,month,1,0,0,0);
        calendar.add(Calendar.MONTH,-1);
        return sdf.format(calendar.getTime());
    }
}
